package com.imut.lagain.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imut.lagain.entity.SystemNotification;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统通知数据访问层
 */
@Mapper
public interface SystemNotificationRepository extends BaseMapper<SystemNotification> {
    
    /**
     * 获取用户的通知列表（分页）
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "receiver_id = #{userId} AND is_deleted = 0 " +
            "ORDER BY priority DESC, create_time DESC")
    IPage<SystemNotification> findByReceiverId(Page<SystemNotification> page, 
                                              @Param("userId") Long userId);
    
    /**
     * 获取用户未读通知数量
     */
    @Select("SELECT COUNT(*) FROM system_notification WHERE " +
            "receiver_id = #{userId} AND is_read = 0 AND is_deleted = 0")
    Long countUnreadNotifications(@Param("userId") Long userId);
    
    /**
     * 获取用户指定类型的通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "receiver_id = #{userId} AND notification_type = #{type} " +
            "AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<SystemNotification> findByReceiverIdAndType(@Param("userId") Long userId, 
                                                    @Param("type") String type);
    
    /**
     * 获取用户未读的指定类型通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "receiver_id = #{userId} AND notification_type = #{type} " +
            "AND is_read = 0 AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<SystemNotification> findUnreadByReceiverIdAndType(@Param("userId") Long userId, 
                                                          @Param("type") String type);
    
    /**
     * 标记通知为已读
     */
    @Update("UPDATE system_notification SET is_read = 1, read_time = #{readTime} " +
            "WHERE id = #{notificationId} AND is_deleted = 0")
    int markAsRead(@Param("notificationId") Long notificationId, 
                  @Param("readTime") LocalDateTime readTime);
    
    /**
     * 批量标记用户所有通知为已读
     */
    @Update("UPDATE system_notification SET is_read = 1, read_time = #{readTime} " +
            "WHERE receiver_id = #{userId} AND is_read = 0 AND is_deleted = 0")
    int markAllAsRead(@Param("userId") Long userId, 
                     @Param("readTime") LocalDateTime readTime);
    
    /**
     * 批量标记指定类型的通知为已读
     */
    @Update("UPDATE system_notification SET is_read = 1, read_time = #{readTime} " +
            "WHERE receiver_id = #{userId} AND notification_type = #{type} " +
            "AND is_read = 0 AND is_deleted = 0")
    int markTypeAsRead(@Param("userId") Long userId, 
                      @Param("type") String type,
                      @Param("readTime") LocalDateTime readTime);
    
    /**
     * 获取指定时间范围内的通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "receiver_id = #{userId} " +
            "AND create_time BETWEEN #{startTime} AND #{endTime} " +
            "AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<SystemNotification> findByTimeRange(@Param("userId") Long userId,
                                            @Param("startTime") LocalDateTime startTime,
                                            @Param("endTime") LocalDateTime endTime);
    
    /**
     * 获取高优先级未读通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "receiver_id = #{userId} AND priority >= #{minPriority} " +
            "AND is_read = 0 AND is_deleted = 0 " +
            "ORDER BY priority DESC, create_time DESC")
    List<SystemNotification> findHighPriorityUnread(@Param("userId") Long userId, 
                                                   @Param("minPriority") Integer minPriority);
    
    /**
     * 根据资源ID和类型查找通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "resource_id = #{resourceId} AND resource_type = #{resourceType} " +
            "AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<SystemNotification> findByResource(@Param("resourceId") Long resourceId, 
                                           @Param("resourceType") String resourceType);
    
    /**
     * 获取触发者相关的通知
     */
    @Select("SELECT * FROM system_notification WHERE " +
            "trigger_user_id = #{triggerUserId} " +
            "AND is_deleted = 0 " +
            "ORDER BY create_time DESC")
    List<SystemNotification> findByTriggerUserId(@Param("triggerUserId") Long triggerUserId);
    
    /**
     * 删除过期通知（软删除）
     */
    @Update("UPDATE system_notification SET is_deleted = 1 " +
            "WHERE create_time < #{expireTime}")
    int deleteExpiredNotifications(@Param("expireTime") LocalDateTime expireTime);
    
    /**
     * 获取用户最近的通知摘要
     */
    @Select("SELECT notification_type, COUNT(*) as count " +
            "FROM system_notification " +
            "WHERE receiver_id = #{userId} AND is_read = 0 " +
            "AND create_time >= #{since} AND is_deleted = 0 " +
            "GROUP BY notification_type")
    List<Object> getNotificationSummary(@Param("userId") Long userId, 
                                       @Param("since") LocalDateTime since);
}